<template>
  <div class="container" element-loading-text="加载中">
    <!-- 顶部按钮组 -->
    <topBar @clickRefresh="clickRefresh"></topBar>
    <div class="weighting_room">
      <div class="flex box" v-show="false">
        <div class="box_label">预约车辆</div>
        <div class="invehicle_num_box">
          <el-button
            class="platenum"
            v-for="item in orderList"
            @click="clickPlateNo(item, 'order')"
            :key="item.id_"
            type="warning"
            size="medium"
            >{{ item.vehicle_num || "未识别" }}
          </el-button>
        </div>
      </div>
      <div class="flex box">
        <div class="box_label">场内车辆</div>
        <div class="invehicle_num_box">
          <el-button
            class="platenum"
            :class="activeId == item.id_ ? 'blue_btn' : 'lightblue_btn'"
            v-for="item in innerList"
            @click="clickPlateNo(item, 'inner')"
            :key="item.id_"
            type="warning"
            size="medium"
            >{{ item.vehicle_num || "未识别" }}
          </el-button>
        </div>
      </div>
      <div class="flex box">
        <div class="box_label">准备出场</div>
        <div class="vehicle_num_box">
          <el-button
            class="platenum"
            :class="[
              activeId == item.id_
                ? item.status === 9
                  ? 'gray_btn'
                  : 'green_btn'
                : item.status === 9
                ? 'lightgray_btn'
                : 'lightgreen_btn'
            ]"
            v-for="item in outList"
            :key="item.id_"
            @click="clickPlateNo(item, 'outer')"
            size="medium"
          >
            {{ item.vehicle_num || "未识别" }}
          </el-button>
        </div>
      </div>
      <div class="flex box">
        <div class="box_label">异常车辆</div>
        <div class="invehicle_num_box">
          <el-button
            class="platenum errorbtn"
            :class="[
              activeId == item.id_
                ? item.origin === 'emptyLocation'
                  ? 'blue_btn'
                  : 'green_btn'
                : item.origin === 'emptyLocation'
                ? 'lightblue_btn'
                : 'lightgreen_btn'
            ]"
            v-for="item in abnormalList"
            @click="clickPlateNo(item, 'error')"
            :key="item.id_"
            size="medium"
          >
            {{ item.vehicle_num || "未识别" }}
          </el-button>
        </div>
      </div>
      <div class="flex box">
        <div class="box_label">近期出场</div>
        <div class="invehicle_num_box">
          <el-button
            class="platenum"
            :class="[
              activeId == item.id_ ? 'gray_btn' : 'lightgray_btn',
              item.status < -1 ? 'errorbtn' : ''
            ]"
            v-for="item in recentList"
            @click="clickPlateNo(item, 'recent')"
            :key="item.id_"
            size="medium"
            >{{ item.vehicle_num || "未识别" }}
          </el-button>
        </div>
        <el-button
          v-if="recentTotal > 8"
          type="info"
          icon="el-icon-more"
          class="more_btn"
          circle
          @click="showTableDrawer('recent')"
        ></el-button>
      </div>
      <div class="box" ref="formBox">
        <el-form
          ref="form"
          :model="form"
          label-width="190px"
          :rules="rules"
          label-position="left"
          size="medium "
        >
          <el-row :gutter="40">
            <el-col :xs="24" :sm="24" :lg="12">
              <el-form-item label="单号：">
                {{ form.gross_device_num || "" }}
              </el-form-item>
              <el-row>
                <el-col :span="9" class="province_box">
                  <el-form-item label="车牌号码：">
                    <el-select
                      class="province_select"
                      :disabled="
                        Boolean(activeId) &&
                          !['inner', 'error'].includes(activeType)
                      "
                      v-model="form.province"
                      popper-class="province_select_style"
                      placeholder=""
                      @change="handleProSelect"
                    >
                      <el-option
                        v-for="item in provinceList"
                        :key="item"
                        :label="item"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="7" class="license_box">
                  <el-form-item label-width="5px">
                    <el-autocomplete
                      ref="license"
                      class="w_full"
                      :disabled="
                        Boolean(activeId) &&
                          !['inner', 'error'].includes(activeType)
                      "
                      v-model="form.licensePlate"
                      popper-class="license_select_style"
                      :fetch-suggestions="queryLicenseSearch"
                      @select="handleLicenseSelect"
                      @change="checkLicenseSelect"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="16">
                  <el-form-item label="货物名称：" prop="payload">
                    <el-select
                      class="w_full"
                      :disabled="
                        (Boolean(activeId) &&
                          !['order', 'inner', 'outer', 'error'].includes(
                            activeType
                          )) ||
                          activeItem.status == 9
                      "
                      v-model="form.payload"
                      popper-class="payload_select_style"
                      placeholder="请选择"
                      @change="payloadChange"
                    >
                      <el-option
                        v-for="(item, index) in goodSpec"
                        :key="item.id_"
                        :label="item.payload_spec"
                        :value="item.payload_spec"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label-width="15px">
                    <el-radio-group
                      v-model="form.order_type"
                      :disabled="
                        (Boolean(activeId) &&
                          !['order', 'inner', 'outer', 'error'].includes(
                            activeType
                          )) ||
                          activeItem.status == 9
                      "
                      @input="handleOrderType"
                      class="orderRadio flex space_around"
                      v-removeAriaHidden
                    >
                      <el-radio :label="1" border>销售</el-radio>
                      <el-radio :label="2" border>采购/退货</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item
                label="生产线："
                :prop="form.order_type == 1 ? 'vender' : 'customer'"
              >
                <el-select
                  v-if="form.order_type == 1"
                  v-model="form.vender"
                  filterable
                  :disabled="
                    (Boolean(activeId) &&
                      !['order', 'inner', 'outer', 'error'].includes(
                        activeType
                      )) ||
                      activeItem.status == 9
                  "
                  popper-class="payload_select_style"
                  class="w_full"
                >
                  <el-option
                    v-for="(item, index) in proLines"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
                <el-select
                  v-else
                  v-model="form.customer"
                  filterable
                  :disabled="
                    (Boolean(activeId) &&
                      !['order', 'inner', 'outer', 'error'].includes(
                        activeType
                      )) ||
                      activeItem.status == 9
                  "
                  popper-class="payload_select_style"
                  class="w_full"
                >
                  <el-option
                    v-for="(item, index) in proLines"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                v-if="form.order_type == 1 || form.order_type == 2"
                :label="form.order_type == 1 ? '收货单位：' : '发货单位：'"
                :prop="form.order_type == 1 ? 'customer' : 'vender'"
              >
                <el-select
                  v-if="form.order_type == 1"
                  v-model="form.customer"
                  filterable
                  :disabled="
                    (Boolean(activeId) &&
                      !['order', 'inner', 'outer', 'error'].includes(
                        activeType
                      )) ||
                      activeItem.status == 9
                  "
                  popper-class="payload_select_style"
                  class="w_full"
                  @change="handleCusVenSelect"
                >
                  <el-option
                    v-for="(item, index) in customers"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
                <el-select
                  v-else
                  v-model="form.vender"
                  filterable
                  :disabled="
                    (Boolean(activeId) &&
                      !['order', 'inner', 'outer', 'error'].includes(
                        activeType
                      )) ||
                      activeItem.status == 9
                  "
                  popper-class="payload_select_style"
                  class="w_full"
                  @change="handleCusVenSelect"
                >
                  <el-option
                    v-for="(item, index) in suppliers"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item v-show="false" prop="payload_id">
                {{ form.payload_id }}
              </el-form-item>
              <el-form-item v-show="false" prop="customer_vender_id">
                {{ form.customer_vender_id }}
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :lg="12">
              <div v-if="activeType == 'error'" class="error_weight_box fl">
                <div class="result_text">
                  <i class="el-icon-info"></i>
                  {{ chooseResult }}
                  <span v-if="netWeight <= 0" style="color:#ff5151">
                    <i class="el-icon-warning"></i>
                    净重异常，请确认！
                  </span>
                </div>
                <div class="error_weight_content fl">
                  <div
                    v-for="(item, index) of form.weiList"
                    :key="index"
                    class="weight_item fl"
                  >
                    <div class="weight_info_box fl">
                      <div class="flex_bet">
                        <div class="flex_col_around">
                          <div>
                            <span class="num">{{ item.weight || 0 }}</span>
                            <span>吨</span>
                          </div>
                          <div class="time">
                            {{ item.time }}
                          </div>
                        </div>
                        <div class="flex_col_center">
                          <el-button
                            type="primary"
                            plain
                            @click="
                              checkAcquirepicture(
                                'error',
                                item.device_code,
                                item.device_num
                              )
                            "
                            >称重照片</el-button
                          >
                        </div>
                      </div>
                    </div>
                    <div class="weight_btn_box fl">
                      <el-button
                        :class="[
                          'weight_blue',
                          grossIndex == index
                            ? 'active'
                            : grossIndex == null
                            ? ''
                            : 'disable'
                        ]"
                        type="primary"
                        size="small"
                        plain
                        @click="chooseGross(index)"
                        >重车</el-button
                      >
                      <el-button
                        :class="[
                          'weight_yellow',
                          tareIndex == index
                            ? 'active'
                            : tareIndex == null
                            ? ''
                            : 'disable'
                        ]"
                        type="primary"
                        size="small"
                        plain
                        @click="chooseTare(index)"
                        >空车</el-button
                      >
                    </div>
                  </div>
                </div>
              </div>
              <el-form-item
                v-if="activeType != 'error'"
                :label="
                  activeType == 'inner' || !activeType
                    ? '进场时间：'
                    : activeType == 'recent'
                    ? '离场时间：'
                    : '毛重时间：'
                "
              >
                <span class="time_row">
                  {{
                    activeType == "inner" || !activeType
                      ? form.create_time
                      : activeType == "recent"
                      ? form.leave_time
                      : form.gross_time
                  }}
                </span>
                <el-button
                  v-if="
                    Boolean(activeId) &&
                      ['inner', 'recent'].includes(activeType)
                  "
                  type="primary"
                  plain
                  @click="
                    checkEntrancePic(
                      form.enter_device_num,
                      form.enter_device_code,
                      form.leave_device_num,
                      form.leave_device_code
                    )
                  "
                  >进出照片</el-button
                >
                <el-row>
                  <span
                    v-if="activeType == 'recent' && form.status < -1"
                    style="color:#ff5151"
                  >
                    <i class="el-icon-warning"></i>
                    {{ form.status | recentErrorText }}
                  </span>
                </el-row>
              </el-form-item>
              <el-form-item v-if="activeType != 'error'" label="毛重：">
                <el-input
                  class="import_num"
                  :value="form.gross_weight || 0"
                  disabled
                >
                  <span slot="append"> 吨</span>
                </el-input>
                <el-button
                  v-if="
                    activeId &&
                      (activeId != '') &
                        (form.gross_device_num && form.gross_device_num != '') &
                        (form.gross_device_code && form.gross_device_code != '')
                  "
                  type="primary"
                  plain
                  @click="checkAcquirepicture('gross')"
                  >称重照片</el-button
                >
              </el-form-item>
              <el-form-item v-if="activeType != 'error'" label="皮重：">
                <el-input
                  class="import_num"
                  :value="form.tare_weight || 0"
                  disabled
                >
                  <span slot="append"> 吨</span>
                </el-input>
                <el-button
                  v-if="
                    activeId &&
                      (activeId != '') &
                        (form.tare_device_num && form.tare_device_num != '') &
                        (form.tare_device_code && form.tare_device_code != '')
                  "
                  type="primary"
                  plain
                  @click="checkAcquirepicture('tare')"
                  >称重照片</el-button
                >
              </el-form-item>
              <el-form-item v-if="activeType != 'error'" label="净重：">
                <el-input
                  class="import_num"
                  :value="form.net_weight || 0"
                  disabled
                >
                  <span slot="append"> 吨</span>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="tool_btn">
          <div>
            <el-button
              @click="clickAdd"
              class="lightblue_btn"
              icon="el-icon-document-add"
              >新建磅单</el-button
            >
            <el-button
              @click="clickIn"
              class="blue_btn"
              :loading="inloading"
              icon="el-icon-circle-check"
              v-if="isNew"
              >确认进场</el-button
            >
          </div>
          <div>
            <template v-if="activeType == 'inner' || activeType == 'outer'">
              <el-button
                v-if="activeItem.status != 9"
                @click="clickUpdate"
                class="lightblue_btn"
                icon="el-icon-edit-outline"
              >
                磅单更正
              </el-button>
              <el-button
                v-if="activeItem.status != 9"
                @click="clickCancel"
                class="lightred_btn"
                :loading="outloading"
                icon="el-icon-document-delete"
                >磅单作废</el-button
              >
              <el-button
                v-else-if="activeItem.status == 9"
                @click="clickCancelPrint"
                class="lightred_btn"
                :loading="outloading"
                icon="el-icon-document-delete"
                >作废重开</el-button
              >
            </template>

            <template v-if="activeType == 'error'"> </template>
            <!-- activeType == 'recent' 已出场的不能打印 -->
            <el-button
              v-if="activeType == 'outer'"
              @click="clickPrint"
              class="green_btn"
              icon="el-icon-printer"
              >打印磅单</el-button
            >
          </div>
        </div>
      </div>
      <!-- 打印 -->
      <Print ref="print" @refresh="clickRefresh" :activeType="activeType" />

      <!-- 进出场照片 -->
      <el-dialog
        title="进出场照片"
        :modal="false"
        custom-class="picDialog"
        v-dialogDrag
        :visible.sync="entrancePicShow"
        @close="entrancePicClose"
      >
        <entrancePicDialog
          v-if="entrancePicShow"
          :formData="entrancePicForm"
          @refresh="checkEntrancePic"
        ></entrancePicDialog>
      </el-dialog>

      <!-- 称重照片 -->
      <el-dialog
        title="称重照片"
        :modal="false"
        custom-class="picDialog"
        v-dialogDrag
        :visible.sync="picShow"
        @close="picClose"
      >
        <picDialog
          :formData="picForm"
          :weightType="weightPicType"
          :loading="picloading"
          @refresh="checkAcquirepicture"
        ></picDialog>
      </el-dialog>

      <!-- enter异常车辆 -->
      <el-dialog
        :modal="false"
        custom-class="enterDialog"
        :visible.sync="enterPicShow"
        :show-close="false"
        :destroy-on-close="true"
      >
        <el-row class="pic_text">
          <i class="el-icon-warning-outline"></i>
          车辆申请进场
        </el-row>
        <div class="pic_info_box">
          <el-row class="info_item">
            <span class="key">
              <span>车</span>
              <span>牌</span>
              <span>号</span>
            </span>
            <span class="value">{{ enterData.vehicle_num || "未识别" }}</span>
          </el-row>
          <el-row class="info_item">
            <span class="key">
              <span>称</span>
              <span>重</span>
              <span>时</span>
              <span>间</span>
            </span>
            <span class="value">{{ enterData.time }}</span>
          </el-row>
        </div>
        <div class="pic_viewer">
          <el-image
            v-for="(item, index) in enterData.weightPicMin"
            :key="index"
            class="img"
            fit="cover"
            :src="item"
            :preview-src-list="privewImages(enterData.weightPic, index)"
          >
          </el-image>
        </div>
        <div class="pic_toolbar">
          <el-button
            type="success"
            icon="el-icon-check"
            @click="handleEnterLeave(enterData.num, 'enter', true)"
            >允许通行</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-close"
            @click="handleEnterLeave(enterData.num, 'enter', false)"
            >禁止通行</el-button
          >
        </div>
      </el-dialog>

      <!-- leave异常车辆 -->
      <el-dialog
        :modal="false"
        custom-class="leaveDialog"
        :visible.sync="leavePicShow"
        :show-close="false"
        :destroy-on-close="true"
      >
        <el-row class="pic_text">
          <i class="el-icon-warning-outline"></i>
          车辆申请离场
        </el-row>
        <div class="pic_info_box">
          <el-row class="info_item">
            <span class="key">
              <span>车</span>
              <span>牌</span>
              <span>号</span>
            </span>
            <span class="value">{{ leaveData.vehicle_num }}</span>
          </el-row>
          <el-row class="info_item">
            <span class="key">
              <span>称</span>
              <span>重</span>
              <span>时</span>
              <span>间</span>
            </span>
            <span class="value">{{ leaveData.time }}</span>
          </el-row>
        </div>
        <div class="pic_viewer">
          <el-image
            v-for="(item, index) in leaveData.weightPicMin"
            :key="index"
            class="img"
            fit="cover"
            :src="item"
            :preview-src-list="privewImages(leaveData.weightPic, index)"
          >
          </el-image>
        </div>
        <div class="pic_toolbar">
          <el-button
            type="success"
            icon="el-icon-check"
            @click="handleEnterLeave(leaveData.num, 'leave', true)"
            >允许通行</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-close"
            @click="handleEnterLeave(leaveData.num, 'leave', false)"
            >禁止通行</el-button
          >
        </div>
      </el-dialog>

      <!--异常/最近出场车辆详情表格-->
      <el-drawer
        :title="tableDrawer.title"
        :modal-append-to-body="false"
        custom-class="listDrawer"
        :visible.sync="tableDrawerShow"
        :before-close="closeListDrawer"
        :destroy-on-close="true"
        :size="1000"
      >
        <table-drawer
          ref="tableDrawerRef"
          :type="tableDrawer.type"
          @reClickPrint="reClickPrint"
          @reviewCar="reviewCar"
        ></table-drawer>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import {
  getList,
  getListJson,
  getRequest,
  getLoadmeterOrder,
  editLoadmeter,
  reWeightPrint
} from "@/api/public";
import { getUser } from "@/utils/auth";
import _ from "lodash";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
// 第三方
import moment from "moment";
// 组件
import topBar from "@/components/theTopBar";
import entrancePicDialog from "@/components/entrancePicDialog";
import picDialog from "@/components/picDialog";
import Print from "./print";
import TableDrawer from "@/components/tableDrawer.vue";

export default {
  components: {
    ElImageViewer,
    topBar,
    entrancePicDialog,
    picDialog,
    Print,
    TableDrawer
  },
  data() {
    return {
      // 异常车辆-多条称重记录-选中的空车/重车的下标
      grossIndex: null,
      tareIndex: null,
      // 抽屉表格相关数据
      tableDrawerShow: false,
      tableDrawer: {
        title: "",
        type: "" // error-异常车辆 recent-近期出场车辆
      },
      // 进出场照片dialog相关数据
      // dialog显示隐藏
      entrancePicShow: false,
      // dialog数据
      entrancePicForm: {
        vehicle_num: 0,
        enter_time: 0,
        leave_time: 0,
        enter: {
          device_code: "",
          device_num: ""
        },
        leave: {
          device_code: "",
          device_num: ""
        }
      },
      // 称重照片dialog相关数据
      // 数据请求loading
      picloading: false,
      // dialog显示隐藏
      picShow: false,
      // 毛重还是皮重还是异常
      weightPicType: "",
      // dialog数据
      picForm: {
        vehicle_num: 0,
        time: 0,
        weight: 0,
        origin: "",
        device_code: "",
        device_num: "",
        weightPicMin: [],
        weightPic: []
      },
      // 预约车辆
      orderList: [],
      // 进场-异常车辆
      enterPicShow: false,
      enterData: {},
      // 离场-异常车辆
      leavePicShow: false,
      leaveData: {},

      // 定时刷新车辆列表，15s刷新一次
      timer: null,
      // 定时刷新异常车辆列表，5s刷新一次
      abNorTimer: null,
      // 定时刷新用户token，30min刷新一次
      tokenTimer: null,
      list: [],
      inloading: false,
      outloading: false,
      loading: false,
      activeId: null,
      activeItem: {},
      activeType: "",

      form: {},
      customers: [],
      suppliers: [],
      // 近期出场
      recentList: [],
      recentTotal: 0,
      // 异常车辆
      // 异常车辆原始数据
      abnormalData: [],
      // 异常车辆合并后的展示数据
      abnormalList: [],
      abnormalTotal: 0,
      goodSpec: [],
      proLines: [],
      // 没有台
      provinceList: [
        "京",
        "津",
        "黑",
        "吉",
        "辽",
        "冀",
        "豫",
        "鲁",
        "晋",
        "陕",
        "蒙",
        "宁",
        "甘",
        "新",
        "青",
        "藏",
        "鄂",
        "皖",
        "苏",
        "沪",
        "浙",
        "闽",
        "湘",
        "赣",
        "川",
        "渝",
        "贵",
        "云",
        "粤",
        "桂",
        "琼",
        "港",
        "澳"
      ],
      // 车牌上没有字母“I”和“O”
      letterList: [
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "J",
        "K",
        "L",
        "M",
        "N",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z"
      ],
      numList: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      lastLicenseInput: "",
      rules: {
        payload: [
          { required: true, message: "请选择货物名称", trigger: "blur,change" }
        ],
        order_type: [
          {
            required: true,
            message: "请选择销售 / 采购/退货",
            trigger: "blur,change"
          }
        ]
      },
      innerList: [],
      outList: [],
      // 是否是新增状态
      isNew: false
    };
  },
  filters: {
    recentErrorText(status) {
      let t = "";
      switch (status) {
        // case -1:
        //   t = "正常离场";
        //   break;
        case -2:
          t = "异常离场";
          break;
        case -3:
          t = "人工补录";
          break;
        case -4:
          t = "人工作废";
          break;
        case -5:
          t = "自动作废";
          break;

        default:
          break;
      }
      return t;
    }
  },
  computed: {
    weightPicMinList() {
      return this.weightPicType == "gross"
        ? this.picForm.grossPicMin
        : this.picForm.tarePicMin;
    },
    weightPicList() {
      return this.weightPicType == "gross"
        ? this.picForm.grossPic
        : this.picForm.tarePic;
    },
    grossResult() {
      let s = "";
      if (this.grossIndex != null) {
        // 选了毛重
        s = `毛重${this.form.weiList[this.grossIndex].weight}吨`;
      } else {
        s = `毛重未选择`;
      }
      return s;
    },
    tareResult() {
      let s = "";
      if (this.tareIndex != null) {
        // 选了皮重
        s = `皮重${this.form.weiList[this.tareIndex].weight}吨`;
      } else {
        s = `皮重未选择`;
      }
      return s;
    },
    netWeight() {
      if (this.grossIndex != null && this.tareIndex != null) {
        return (
          this.form.weiList[this.grossIndex].weight -
          this.form.weiList[this.tareIndex].weight
        );
      } else {
        return 1;
      }
    },
    netResult() {
      let s = "";
      if (this.grossIndex != null && this.tareIndex != null) {
        s = `, 净重${this.netWeight.toFixed(2)}吨`;
      } else {
        s = ``;
      }
      return s;
    },
    chooseResult() {
      return `${this.grossResult}, ${this.tareResult}${this.netResult}`;
    }
  },
  watch: {
    "form.licensePlate": {
      handler() {
        this.lastLicenseInput = this.form.licensePlate || "";
      }
    },
    "tableDrawer.type": {
      handler() {
        this.tableDrawer.title =
          this.tableDrawer.type == "error" ? "异常车辆" : "近期出场";
      }
    }
  },
  created() {
    this.$store
      .dispatch("user/loginEIP")
      .then(response => {
        // 获取所有货物
        this.getAllGood();
        // 获取所有购货单位（客户）
        this.getAllCus();
        // 获取所有发货单位（供货商）
        this.getAllSup();
        // 获取所有生产线
        this.getAllLines();
        // 获取所有非异常车辆;
        this.getCarList();
        // 获取所有异常车辆
        this.getAbnormalList();
        // 每隔15s刷新列表
        this.timer = setInterval(() => {
          // 获取所有车辆;
          this.getCarList();
        }, 15000);

        // 每隔5s刷新列表
        this.abNorTimer = setInterval(() => {
          // 获取所有异常车辆
          this.getAbnormalList();
        }, 5000);

        // 每隔30min刷新token
        this.tokenTimer = setInterval(() => {
          this.$store.dispatch("user/loginEIP").then(response => {});
        }, 1800000);
      })
      .catch(error => {});
  },
  beforeDestroy() {
    if (this.timer) clearInterval(this.timer);
    if (this.tokenTimer) clearInterval(this.tokenTimer);
    if (this.abNorTimer) clearInterval(this.abNorTimer);
  },
  mounted() {},
  methods: {
    // 异常车辆、近期出场车辆表格抽屉页
    showTableDrawer(type) {
      this.tableDrawerShow = true;
      this.tableDrawer.type = type;
    },
    closeListDrawer() {
      this.tableDrawerShow = false;
    },
    // 货品
    getAllGood() {
      getListJson({
        alias: "payload",
        queryFilter: {
          pageBean: { page: "1", pageSize: 99, showTotal: "true" },
          sorter: [{ property: "status", direction: "DESC" }]
        }
      })
        .then(response => {
          this.goodSpec = response.rows || [];
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 购货单位（客户）
    getAllCus() {
      getListJson({
        alias: "customer",
        queryFilter: {
          pageBean: { page: "1", pageSize: 500, showTotal: "true" },
          sorter: [{ property: "num", direction: "DESC" }]
        }
      }).then(response => {
        this.customers = response.rows || [];
      });
    },
    // 发货单位（供货商）
    getAllSup() {
      getListJson({
        alias: "vendor",
        queryFilter: {
          pageBean: { page: "1", pageSize: 500, showTotal: "true" },
          sorter: [{ property: "num", direction: "DESC" }]
        }
      }).then(response => {
        this.suppliers = response.rows || [];
      });
    },
    //
    handleCusVenSelect(item) {
      let list = this.form.order_type == 1 ? this.customers : this.suppliers;
      this.form.customer_vender_id = list.find(e => e.name == item).id_;
    },
    handleOrderType(value) {
      // 生产线和收/发货单位的下拉菜单值会直接互换
      // 防止不在列表中
      // 暂时先全清空
      this.$set(this.form, "customer", "");
      this.$set(this.form, "vender", "");
      this.$set(this.form, "customer_vender_id", "");
      if (this.activeType == "error") {
        // -有匹配车辆
        //   -重车、空车都有了
        //     -保持不变
        //   -只有重车/空车
        //     -已有车辆的保持不变 在剩下的异常记录里重新选默认值
        // -没有匹配车辆
        //   -在剩下的异常记录里重新选默认值
        if (!this.form.weiList.find(e => e.type == "gross")) {
          this.grossIndex = null;
        }
        if (!this.form.weiList.find(e => e.type == "tare")) {
          this.tareIndex = null;
        }
        this.intTypeIndex(this.form.order_type, this.form.weiList);
      }
    },
    // 生产线
    getAllLines() {
      getListJson({
        alias: "line",
        queryFilter: {
          pageBean: { page: "1", pageSize: 100, showTotal: "true" }
        }
      }).then(response => {
        this.proLines = response.rows || [];
      });
    },
    // 查近期出场车辆
    getRecentList() {
      getListJson({
        alias: "loadometer_out",
        queryFilter: {
          pageBean: { page: "1", pageSize: 8, showTotal: "true" },
          sorter: [{ property: "leave_time", direction: "DESC" }]
        }
      })
        .then(response => {
          this.recentList = response.rows || [];
          this.recentTotal = response.total || 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 查预约车辆
    getOrderList() {
      getLoadmeterOrder().then(response => {
        this.orderList = response.value || [];
      });
    },
    // 查场内车辆
    getInnerList() {
      getListJson({
        alias: "loadometer_in",
        queryFilter: {
          pageBean: { page: "1", pageSize: 100, showTotal: "true" },
          sorter: [{ property: "enter_time", direction: "DESC" }]
        }
      }).then(response => {
        this.innerList = response.rows || [];
      });
    },
    // 查准备出场车辆
    getOutList() {
      getListJson({
        alias: "loadometer_ready",
        queryFilter: {
          pageBean: { page: "1", pageSize: 200, showTotal: "true" },
          sorter: [{ property: "gross_time", direction: "DESC" }]
        }
      }).then(response => {
        this.outList = response.rows || [];
      });
    },
    //异常车辆
    getAbnormalList() {
      // ?page=1&pageSize=8
      getRequest("mine/loadometer/exceptions").then(response => {
        // 普通异常
        // 根据车牌号 把所有记录处理成 车牌号-称重记录数组（按照时间由近到远排序）
        // 车牌未识别/为空的 跳过处理
        let list = [];
        response.rows.map(e => {
          e.id_ = e.device_num;
          e.gross_device_num = e.device_num;
          if (e.vehicle_num == "" || e.vehicle_num == "未识别") {
            e.weiList = [{ ...e }];
            list.push(e);
          } else {
            let i = list.findIndex(j => j.vehicle_num == e.vehicle_num);
            // console.log(list);
            if (i > -1) {
              list[i].weiList.push({
                ...e
              });
            } else {
              list.push({
                ...e,
                weiList: [
                  {
                    ...e
                  }
                ]
              });
            }
          }
        });
        this.abnormalTotal = response.total;
        this.abnormalData = response.rows;
        this.abnormalList = list;
        // 入场异常
        if (response.requests.enter.num) {
          if (this.enterPicShow) {
            return;
          } else {
            const {
              device_code,
              device_num,
              num,
              time,
              vehicle_num
            } = response.requests.enter;

            this.$set(this.enterData, "device_code", device_code);
            this.$set(this.enterData, "device_num", device_num);
            this.$set(this.enterData, "num", num);
            this.$set(this.enterData, "time", time);
            this.$set(this.enterData, "vehicle_num", vehicle_num);
            if (this.enterData.device_code && this.enterData.device_num) {
              getList(
                {
                  device_code: this.enterData.device_code,
                  device_num: this.enterData.device_num
                },
                "/mine/iot/capture"
              )
                .then(response => {
                  if (response.state) {
                    this.enterData.weightPicMin =
                      response.value.images.map(e => {
                        if (e && e.indexOf("?") > -1) {
                          return e + "&height=170";
                        } else {
                          return e + "?height=170";
                        }
                      }) || [];
                    this.enterData.weightPic = response.value.images || [];
                    this.enterPicShow = true;
                  } else {
                    this.$set(this.enterData, "weightPicMin", []);
                    this.$set(this.enterData, "weightPic", []);
                    this.$message.error(response.message);
                  }
                })
                .catch(error => {
                  this.$set(this.enterData, "weightPicMin", []);
                  this.$set(this.enterData, "weightPic", []);
                  this.$message.error(response.message);
                })
                .finally(() => {
                  // setTimeout(() => {
                  //   this.picloading = false;
                  // }, 1000);
                });
            } else {
              this.$set(this.enterData, "weightPicMin", []);
              this.$set(this.enterData, "weightPic", []);
              this.$set(this, "enterPicShow", true);
            }
          }
        } else {
          if (this.enterPicShow) {
            this.enterPicShow = false;
          }
        }

        // 离场异常
        if (response.requests.leave.num) {
          if (this.leavePicShow) {
            return;
          } else {
            const {
              device_code,
              device_num,
              num,
              time,
              vehicle_num
            } = response.requests.leave;

            this.$set(this.leaveData, "device_code", device_code);
            this.$set(this.leaveData, "device_num", device_num);
            this.$set(this.leaveData, "num", num);
            this.$set(this.leaveData, "time", time);
            this.$set(this.leaveData, "vehicle_num", vehicle_num);
            if (this.leaveData.device_code && this.leaveData.device_num) {
              getList(
                {
                  device_code: this.leaveData.device_code,
                  device_num: this.leaveData.device_num
                },
                "/mine/iot/capture"
              )
                .then(response => {
                  if (response.state) {
                    this.leaveData.weightPicMin =
                      response.value.images.map(e => {
                        if (e && e.indexOf("?") > -1) {
                          return e + "&height=170";
                        } else {
                          return e + "?height=170";
                        }
                      }) || [];
                    this.leaveData.weightPic = response.value.images || [];
                    this.leavePicShow = true;
                  } else {
                    this.$set(this.leaveData, "weightPicMin", []);
                    this.$set(this.leaveData, "weightPic", []);
                    this.$message.error(response.message);
                  }
                })
                .catch(error => {
                  this.$set(this.leaveData, "weightPicMin", []);
                  this.$set(this.leaveData, "weightPic", []);
                  this.$message.error(response.message);
                })
                .finally(() => {
                  // setTimeout(() => {
                  //   this.picloading = false;
                  // }, 1000);
                });
            } else {
              this.$set(this.leaveData, "weightPicMin", []);
              this.$set(this.leaveData, "weightPic", []);
              this.leavePicShow = true;
            }
          }
        } else {
          if (this.leavePicShow) {
            this.leavePicShow = false;
          }
        }
      });
    },
    // 调整图片顺序，把当前图片放在第一位
    privewImages(l, index) {
      let list = [...l];
      if (index == 0) return list;
      let start = list.splice(index);
      let remain = list.splice(0, index);
      return start.concat(remain);
    },
    // 异常车辆申请进场/离场
    handleEnterLeave(num, type, re) {
      getList(
        { location: "梨枫", num, result: re, reason: "理由" },
        `/mine/${type == "enter" ? "checkin" : "checkout"}/review`
      )
        .then(response => {
          this.$message.success("处理成功!");
          this.getAbnormalList();
        })
        .catch(error => {
          this.$message.error(response.message);
        })
        .finally(() => {});
    },
    // 点击车牌号，请求表单信息
    clickPlateNo(item, type) {
      this.isNew = false;
      this.activeId = item.id_;
      this.activeType = type;
      this.activeItem = item;

      let licensePlate = {
        licensePlate: "",
        province: ""
      };
      if (item.vehicle_num && item.vehicle_num != "未识别") {
        licensePlate = {
          licensePlate: item.vehicle_num ? item.vehicle_num.substring(1) : "",
          province: item.vehicle_num ? item.vehicle_num.substring(0, 1) : ""
        };
      }
      this.form = {
        ...item,
        ...licensePlate
      };
      // 异常车辆的查看
      if (type == "error") {
        // 默认开启新建磅单
        this.isNew = true;
        // 先判断 预约车辆+场内车辆+未打印的准备出场 中是否已含有
        let carList = [
          ...this.orderList.map(e => e.vehicle_num),
          ...this.innerList.map(e => e.vehicle_num),
          ...this.outList.filter(e => e.status == 8).map(e => e.vehicle_num)
        ];
        let carNo = item.vehicle_num;
        if (
          !item.vehicle_num ||
          item.vehicle_num == "" ||
          item.vehicle_num == "未识别"
        ) {
          carNo = "";
        }
        // 一个车牌号对应多条称重记录
        // 需要选择一个毛重重量 一个皮重重量
        this.grossIndex = null;
        this.tareIndex = null;
        if (carList.includes(carNo)) {
          // 若有 询问是否代入已有值 货物名称、销售采购、发货单位、收货单位、车牌号、称重记录
          this.$confirm(
            "该车辆已预约/进场，是否将货物、单位、称重信息填入",
            "提示",
            {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }
          )
            .then(() => {
              // 获取预约/场内已有相同的车牌号的信息
              let info = [
                ...this.orderList,
                ...this.innerList,
                ...this.outList.filter(e => e.status == 8)
              ].filter(e => e.vehicle_num == carNo);
              this.grossIndex = null;
              this.tareIndex = null;

              // 同车牌号的称重数据插入list
              // 但直接push会重复
              // 暂时想的是判断前两条记录的设备number和已有车的称重记录的设备number
              if (info[0].gross_device_num && info[0].gross_device_code) {
                if (
                  item.weiList.find(
                    e => e.device_num == info[0].gross_device_num
                  )
                ) {
                  //
                } else {
                  item.weiList.push({
                    time: info[0].gross_time,
                    weight: info[0].gross_weight,
                    device_num: info[0].gross_device_num,
                    device_code: info[0].gross_device_code,
                    type: "gross"
                  });
                }
              }
              if (info[0].tare_device_num && info[0].tare_device_code) {
                if (
                  item.weiList.find(
                    e => e.device_num == info[0].tare_device_num
                  )
                ) {
                  //
                } else {
                  item.weiList.push({
                    time: info[0].tare_time,
                    weight: info[0].tare_weight,
                    device_num: info[0].tare_device_num,
                    device_code: info[0].tare_device_code,
                    type: "tare"
                  });
                }
              }

              // 按照时间排序
              item.weiList.sort(this.dateData("time", false));
              // 获取重车和空车的下标默认值
              if (info[0].gross_device_num && info[0].gross_device_code) {
                item.weiList.map((e, i) => {
                  if (e.device_num == info[0].gross_device_num) {
                    this.grossIndex = i;
                  }
                });
              }
              if (info[0].tare_device_num && info[0].tare_device_code) {
                item.weiList.map((e, i) => {
                  if (e.device_num == info[0].tare_device_num) {
                    this.tareIndex = i;
                  }
                });
              }
              // 设置空车和重车的下标默认值
              if (this.grossIndex != null && this.tareIndex != null) {
                //
              } else {
                this.intTypeIndex(info[0].order_type, item.weiList);
              }

              // 预约磅单没有customer_vender_id
              if (!info[0].customer_vender_id) {
                let list =
                  info[0].order_type == 1 ? this.customers : this.suppliers;
                let name =
                  info[0].order_type == 1 ? info[0].customer : info[0].vender;
                info[0].customer_vender_id = list.find(e => e.name == name).id_;
              }
              this.form = {
                time: item.time,
                weight: item.weight,
                weiList: item.weiList,
                gross_device_num: item.device_num,
                payload: info[0].payload || "",
                payload_id: info[0].payload_id || "",
                order_type: info[0].order_type,
                vender: info[0].vender || "",
                customer: info[0].customer || "",
                customer_vender_id: info[0].customer_vender_id || "",
                ...licensePlate
              };
              this.$refs.form.resetFields();
            })
            .catch(() => {
              // 设置空车和重车的下标默认值
              if (this.grossIndex != null && this.tareIndex != null) {
                //
              } else {
                this.intTypeIndex(1, item.weiList);
              }
              this.form = {
                // 默认勾选销售
                order_type: 1,
                ...licensePlate,
                weight: item.weight,
                time: item.time,
                weiList: item.weiList,
                gross_device_num: item.device_num
              };
              this.$refs.form.resetFields();
            });
        } else {
          // 按照时间排序
          item.weiList.sort(this.dateData("time", false));
          // 设置空车和重车的下标默认值
          if (this.grossIndex != null && this.tareIndex != null) {
            //
          } else {
            this.intTypeIndex(1, item.weiList);
          }
          // 若没有 代入车牌号
          this.form = {
            // 默认勾选销售
            order_type: 1,
            ...licensePlate,
            weight: item.weight,
            time: item.time,
            weiList: item.weiList,
            gross_device_num: item.device_num
          };
          this.$refs.form.resetFields();
        }
      } else if (type == "order") {
        // 预约车辆是没有按钮的 所以能“点击”预约车辆的只可能是【新建磅单】-【修改车牌号】-匹配到预约车辆的场景
        this.isNew = true;
      } else {
        this.$refs.form.resetFields();
      }
    },
    chooseTare(index) {
      // 同一条记录不能同时是空车和重车
      if (index == this.grossIndex) {
        if (this.tareIndex == null) {
          this.tareIndex = index;
          this.grossIndex = null;
          return;
        } else {
          this.$message.error("同条记录不能同时选中重车和空车！");
          return;
        }
      }
      // 点击直接选中 再点击取消选中
      if (index != this.tareIndex) {
        this.tareIndex = index;
      } else {
        this.tareIndex = null;
      }
    },
    chooseGross(index) {
      // 同一条记录不能同时是空车和重车
      if (index == this.tareIndex) {
        if (this.grossIndex == null) {
          this.grossIndex = index;
          this.tareIndex = null;
          return;
        } else {
          this.$message.error("同条记录不能同时选中重车和空车！");
          return;
        }
      }
      // 点击直接选中 再点击取消选中
      if (index != this.grossIndex) {
        this.grossIndex = index;
      } else {
        this.grossIndex = null;
      }
    },
    getInfo(arg) {
      this.loading = true;

      return getRequest(
        `service/form/dataTemplate/v1/getForm/loadometer/loadometer?id=${
          this.activeId
        }&action=get&recordId=&dataKey=loadometer`
      )
        .then(response => {
          let info = response.data.loadometer || {};

          if (info.status == 9) {
            // 已打印
            this.$confirm("该磅单已打印，请确认已作废旧磅单！", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            })
              .then(() => {
                // 请确认已作废旧磅单
                this.form = {
                  ...info,
                  licensePlate: info.vehicle_num
                    ? info.vehicle_num.substring(1)
                    : "",
                  province: info.vehicle_num
                    ? info.vehicle_num.substring(0, 1)
                    : ""
                };
                this.$refs.print.showModal(this.form);
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消",
                  duration: 1000
                });
              });
          } else {
            // 请确认已作废旧磅单
            this.form = {
              ...info,
              licensePlate: info.vehicle_num
                ? info.vehicle_num.substring(1)
                : "",
              province: info.vehicle_num ? info.vehicle_num.substring(0, 1) : ""
            };
            this.$refs.print.showModal(this.form);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 磅单更正
    clickUpdate() {
      if (!this.activeId || !["inner", "outer"].includes(this.activeType)) {
        this.$message.error("请点击选择一个场内车辆或准备出场车辆！");
        return;
      }

      const {
        id_,
        payload,
        payload_id,
        order_type,
        vender,
        customer,
        customer_vender_id,
        province,
        licensePlate
      } = this.form;

      this.$refs.form.validate(valid => {
        if (valid) {
          if (
            // 如果采购，发货单位必填
            this.form.order_type == 2 &&
            (!this.form.vender || this.form.vender == "")
          ) {
            this.$message.error("请选择发货单位");
            return;
          } else if (
            // 如果是销售，收货单位必填
            this.form.order_type == 1 &&
            (!this.form.customer || this.form.customer == "")
          ) {
            this.$message.error("请选择收货单位");
            return;
          }

          let carNo = province + licensePlate;
          // 选了【销售】+【散户】的话 车牌号可以为空（三轮车之类的没有车牌号的车）
          if (
            this.form.order_type == 1 &&
            this.form.customer &&
            this.form.customer.indexOf("散户") > -1
          ) {
            //
          } else {
            // 其他的仍然要进行车牌号的填写和校验
            if (!carNo || carNo == "") {
              this.$message.error("请填写车牌号码");
              return;
            } else {
              let reg = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([ABCEDF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳使领]{1})$/;
              if (!reg.test(carNo)) {
                // 车牌号不规范
                this.$message.error("请填写有效的车牌号码！");
                return;
              }
            }
          }

          // 车牌号是否已存在预约车辆+在场内车辆+未打印的准备出场中
          // !把自己排除在车辆列表中
          let carList = [
            ...this.orderList,
            ...this.innerList.filter(e => e.id_ != id_).map(e => e.vehicle_num),
            ...this.outList
              .filter(e => e.status == 8 && e.id_ != id_)
              .map(e => e.vehicle_num)
          ];
          if (carList.includes(carNo)) {
            this.$message.error(
              "此车牌号码已预约/在场内，请选择对应车辆进行更正"
            );
            return;
          }

          // 场内车辆 或 准备出场车辆
          getRequest(
            `service/form/dataTemplate/v1/getForm/loadometer/loadometer?id=${id_}&action=get&recordId=&dataKey=loadometer`
          ).then(response => {
            let sendObj = {};
            if (response.data.loadometer) {
              if (
                response.data.loadometer.customer_vender_id !=
                  customer_vender_id ||
                response.data.loadometer.payload != payload
              ) {
                sendObj = {
                  order_id: "",
                  contract: "",
                  contract_id: ""
                };
              }
            }
            this.loading = true;
            getList(
              {
                loadometer: {
                  ...sendObj,
                  id_,
                  payload,
                  payload_id,
                  order_type,
                  vender,
                  customer,
                  vehicle_num: carNo,
                  customer_vender: order_type == 1 ? customer : vender,
                  customer_vender_id
                },
                formKey: "loadometer",
                templateKey: "loadometer"
              },
              `service/form/dataTemplate/v1/boSave/loadometer?delDraftId=`
            )
              .then(response => {
                this.$message.success("磅单更正成功!");
                this.clickRefresh();
              })
              .finally(() => {
                this.loading = false;
              });
          });
        }
      });
    },
    // 磅单作废
    clickCancel() {
      const { id_, num, status } = this.form;
      if (status == 9) {
        // 已打印的磅单不能作废
        this.$message.error("该磅单已打印，无法作废！");
        return;
      } else {
        this.$confirm("确定作废此磅单?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.outloading = true;
            getList(
              {
                location: "梨枫",
                loadometer_num: num || 0
              },
              "/mine/loadometer/unbind"
            )
              .then(response => {
                if (response.state) {
                  // 解绑成功
                  this.$message.success(response.message);
                  getList(
                    {
                      loadometer: {
                        id_,
                        status: -4
                      },
                      formKey: "loadometer",
                      templateKey: "loadometer"
                    },
                    `service/form/dataTemplate/v1/boSave/loadometer?delDraftId=`
                  ).then(response => {
                    this.$message.success("磅单作废成功！");
                    this.clickRefresh();
                  });
                } else {
                  this.$message.error(response.message);
                }
              })
              .finally(() => {
                this.outloading = false;
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消",
              duration: 1000
            });
          });
      }
    },
    // 作废重开
    clickCancelPrint() {
      const { id_, status } = this.form;
      if (status == 9) {
        this.$confirm("磅单已打印，请确认已作废旧磅单！", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.outloading = true;
            reWeightPrint(id_)
              .then(response => {
                this.$message.success("磅单作废成功！");
                // this.clickRefresh();
                // 获取所有非异常车辆;
                this.getCarList();
                // 获取所有异常车辆
                this.getAbnormalList();
                // 获取当前车辆的最新状态
                this.refreshActiveItem();
              })
              .finally(() => {
                this.outloading = false;
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消",
              duration: 1000
            });
          });
      } else {
      }
    },
    // 新建表单
    clickAdd() {
      // 清空所有信息
      this.isNew = true;
      this.activeType = null;
      this.activeId = null;
      this.activeItem = {};
      this.form = {
        // 默认勾选销售
        order_type: 1
      };
      this.$refs.form.resetFields();
    },
    //  车辆离场
    async clickOut() {
      this.outloading = true;
      getList(
        {
          loadometer: {
            id_: this.form.id_,
            status: -1
          },
          formKey: "loadometer",
          templateKey: "loadometer"
        },
        `service/form/dataTemplate/v1/boSave/loadometer?delDraftId=`
      )
        .then(response => {
          this.$message.success("车辆离场成功");
          this.clickRefresh();
        })
        .finally(() => {
          this.outloading = false;
        });
    },
    // 重新称重
    resetWeight() {
      if (!this.activeId) {
        this.$message.error("请点击选择一条准备出场的车牌号！");
        return;
      }
      getList({ id: this.activeId }, "weightrecord/hd/correctdata").then(
        res => {
          if (res.status == "success") {
            this.$message.success("重新称重成功");
            this.clickRefresh();
          }
        }
      );
    },
    // 确认进场
    clickIn() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (
            // 如果采购，发货单位必填
            this.form.order_type == 2 &&
            (!this.form.vender || this.form.vender == "")
          ) {
            this.$message.error("请选择发货单位");
            return;
          } else if (
            // 如果是销售，收货单位必填
            this.form.order_type == 1 &&
            (!this.form.customer || this.form.customer == "")
          ) {
            this.$message.error("请选择收货单位");
            return;
          }

          let carNo = this.form.province + this.form.licensePlate;
          // 选了【销售】+【散户】的话 车牌号可以为空（三轮车之类的没有车牌号的车）
          if (
            this.form.order_type == 1 &&
            this.form.customer &&
            this.form.customer.indexOf("散户") > -1
          ) {
            //
          } else {
            // 其他的仍然要进行车牌号的填写和校验
            if (!carNo || carNo == "") {
              this.$message.error("请填写车牌号码");
              return;
            } else {
              let reg = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([ABCEDF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳使领]{1})$/;
              if (!reg.test(carNo)) {
                // 车牌号不规范
                this.$message.error("请填写有效的车牌号码！");
                return;
              }
            }
          }

          // 车牌号有效
          // 车牌号是否已存在在预约车辆+场内车辆+未打印的准备出场中
          let carList = [
            ...this.orderList.map(e => e.vehicle_num),
            ...this.innerList.map(e => e.vehicle_num),
            ...this.outList.filter(e => e.status == 8).map(e => e.vehicle_num)
          ];
          if (carList.includes(carNo)) {
            let info = [
              ...this.orderList,
              ...this.innerList,
              ...this.outList.filter(e => e.status == 8)
            ].filter(e => e.vehicle_num == carNo);
            if (this.activeType == "error") {
              const { weiList } = this.activeItem;
              // 异常车辆的确认进场
              // 如果找到场内已有相同的车牌号
              // 把当前记录中的 重量 绑定磅单
              // 把当前记录中的 货物名称、销售采购、发货单位、收货单位 更新给那个车牌号
              let {
                id_,
                num,
                gross_device_num,
                gross_device_code,
                tare_device_num,
                tare_device_code
              } = info[0];
              // 预约车辆没有这些字段
              if (!gross_device_num) gross_device_num = "";
              if (!gross_device_code) gross_device_code = "";
              if (!tare_device_num) tare_device_num = "";
              if (!tare_device_code) tare_device_code = "";
              const {
                payload,
                payload_id,
                order_type,
                vender,
                customer,
                customer_vender_id
              } = this.form;

              if (
                this.grossIndex == null &&
                this.tareIndex == null &&
                weiList.length > 1
              ) {
                this.$message.error("请选择对应的重车/空车重量！");
                return;
              }

              // 绑定重量
              // 1、先查看旧的已绑定的重量是否需要解绑
              // 如果一个重量都没有选
              // 或者 如果选中的重量和原重量不一致
              // 先要解绑原重量
              if (
                (this.grossIndex == null && gross_device_num != "") ||
                (this.grossIndex != null &&
                  gross_device_num != weiList[this.grossIndex].device_num &&
                  gross_device_num != "")
              ) {
                await getList(
                  {
                    location: "梨枫",
                    loadometer_num: num || 0,
                    device_code: gross_device_code,
                    device_num: gross_device_num
                  },
                  "/mine/loadometer/unbind"
                ).then(response => {
                  if (response.state) {
                    // 解绑成功
                    this.$message.success(response.message);
                  } else {
                    this.$message.error(response.message);
                  }
                });
              }
              if (
                (this.tareIndex == null && tare_device_num != "") ||
                (this.tareIndex != null &&
                  tare_device_num != weiList[this.tareIndex].device_num &&
                  tare_device_num != "")
              ) {
                await getList(
                  {
                    location: "梨枫",
                    loadometer_num: num || 0,
                    device_code: tare_device_code,
                    device_num: tare_device_num
                  },
                  "/mine/loadometer/unbind"
                ).then(response => {
                  if (response.state) {
                    // 解绑成功
                    this.$message.success(response.message);
                  } else {
                    this.$message.error(response.message);
                  }
                });
              }

              // 2、绑定新重量 新！重量
              if (this.grossIndex == null && this.tareIndex == null) {
                // 2.1、重量都没选
              } else if (
                this.grossIndex != null &&
                gross_device_num != weiList[this.grossIndex].device_num &&
                (this.tareIndex == null ||
                  (this.tareIndex != null &&
                    tare_device_num == weiList[this.tareIndex].device_num))
              ) {
                // 2.2、只选择了毛重-重车的重量/皮重-空车保持原重量 毛重-重车是新重量
                getList(
                  {
                    location: "梨枫",
                    loadometer_num: num || 0,
                    vehicle_num: carNo,
                    device_code: weiList[this.grossIndex].device_code,
                    device_num: weiList[this.grossIndex].device_num,
                    type: "重车"
                  },
                  "/mine/loadometer/bind"
                )
                  .then(response => {
                    if (response.state) {
                      // 绑定成功
                      this.$message.success(response.message);
                      // 抬杆
                      // getList(
                      //   { device_code: weiList[this.grossIndex].device_code },
                      //   "/mine/iot/lift"
                      // ).then(response => {});
                    } else {
                      this.$message.error(response.message);
                    }
                  })
                  .catch(error => {});
              } else if (
                this.tareIndex != null &&
                tare_device_num != weiList[this.tareIndex].device_num &&
                (this.grossIndex == null ||
                  (this.grossIndex != null &&
                    gross_device_num == weiList[this.grossIndex].device_num))
              ) {
                // 2.3、只选择了皮重-空车的重量/毛重-重车保持原重量 皮重-空车是新重量
                getList(
                  {
                    location: "梨枫",
                    loadometer_num: num || 0,
                    vehicle_num: carNo,
                    device_code: weiList[this.tareIndex].device_code,
                    device_num: weiList[this.tareIndex].device_num,
                    type: "空车"
                  },
                  "/mine/loadometer/bind"
                )
                  .then(response => {
                    if (response.state) {
                      // 绑定成功
                      this.$message.success(response.message);
                      // 抬杆
                      // getList(
                      //   { device_code: weiList[this.tareIndex].device_code },
                      //   "/mine/iot/lift"
                      // ).then(response => {});
                    } else {
                      this.$message.error(response.message);
                    }
                  })
                  .catch(error => {});
              } else if (
                this.grossIndex != null &&
                this.tareIndex != null &&
                gross_device_num != weiList[this.grossIndex].device_num &&
                tare_device_num != weiList[this.tareIndex].device_num
              ) {
                // 2.4、毛重-重车 和 皮重-空车 都选择了新重量
                // 离现在时间久远的先绑定 再绑定离现在时间近的
                let far = 0,
                  near = 0,
                  farType = "",
                  nearType = "";
                if (this.tareIndex > this.grossIndex) {
                  far = this.tareIndex;
                  near = this.grossIndex;
                  farType = "空车";
                  nearType = "重车";
                } else {
                  far = this.grossIndex;
                  near = this.tareIndex;
                  farType = "重车";
                  nearType = "空车";
                }
                getList(
                  {
                    location: "梨枫",
                    loadometer_num: num || 0,
                    vehicle_num: carNo,
                    device_code: weiList[far].device_code,
                    device_num: weiList[far].device_num,
                    type: farType
                  },
                  "/mine/loadometer/bind"
                )
                  .then(response => {
                    if (response.state) {
                      // 绑定成功
                      this.$message.success(response.message);
                      // 抬杆
                      // getList(
                      //   { device_code: weiList[far].device_code },
                      //   "/mine/iot/lift"
                      // ).then(response => {});
                      getList(
                        {
                          location: "梨枫",
                          loadometer_num: num || 0,
                          vehicle_num: carNo,
                          device_code: weiList[near].device_code,
                          device_num: weiList[near].device_num,
                          type: nearType
                        },
                        "/mine/loadometer/bind"
                      )
                        .then(response => {
                          if (response.state) {
                            // 绑定成功
                            this.$message.success(response.message);
                            // 抬杆
                            // getList(
                            //   { device_code: weiList[near].device_code },
                            //   "/mine/iot/lift"
                            // ).then(response => {});
                          } else {
                            this.$message.error(response.message);
                          }
                        })
                        .catch(error => {});
                    } else {
                      this.$message.error(response.message);
                    }
                  })
                  .catch(error => {});
              }

              // 更新其他信息
              getList(
                {
                  loadometer: {
                    id_,
                    payload,
                    payload_id,
                    order_type,
                    vender,
                    customer,
                    customer_vender: order_type == 1 ? customer : vender,
                    customer_vender_id
                  },
                  formKey: "loadometer",
                  templateKey: "loadometer"
                },
                `service/form/dataTemplate/v1/boSave/loadometer?delDraftId=`
              )
                .then(response => {
                  setTimeout(() => {
                    this.$message.success("磅单更正成功!");
                    this.clickRefresh();
                  }, 2000);
                })
                .finally(() => {});
            } else {
              // 【新建磅单】-【修改车牌号】【匹配上车牌】
              if (info[0].status == 0) {
                // 预约车辆可以【确认进场】
                // 更新信息和状态改为1
                let { id_ } = info[0];
                const {
                  payload,
                  payload_id,
                  order_type,
                  vender,
                  customer,
                  customer_vender_id
                } = this.form;
                getList(
                  {
                    loadometer: {
                      id_,
                      payload,
                      payload_id,
                      order_type,
                      vender,
                      customer,
                      customer_vender: order_type == 1 ? customer : vender,
                      customer_vender_id,
                      status: 1
                    },
                    formKey: "loadometer",
                    templateKey: "loadometer"
                  },
                  `service/form/dataTemplate/v1/boSave/loadometer?delDraftId=`
                )
                  .then(response => {
                    setTimeout(() => {
                      this.$message.success("进场成功!");
                      this.clickRefresh();
                    }, 2000);
                  })
                  .finally(() => {});
                xiaohan;
              } else {
                // 场内车辆+准备出场 理论上是不会有【新建磅单】-【匹配车牌号】的【确认进场】 以防万一
                this.$message.error("此车牌号码在场内，请勿重复进场");
                return;
              }
            }
          } else {
            // 如果没有相同的车牌号 新建
            this.inloading = true;

            // 1、如果是直接点击【新建磅单】（即非异常车辆）
            // 新建磅单的服务走node服务
            // 2、如果是其他情况
            // 新建磅单的服务走eip服务
            if (!this.activeId) {
              const {
                payload,
                payload_id,
                order_type,
                customer,
                vender,
                customer_vender_id
              } = this.form;
              editLoadmeter({
                id_: "",
                payload,
                payload_id,
                order_type,
                vender,
                customer,
                vehicle_num: carNo,
                customer_vender: order_type == 1 ? customer : vender,
                customer_vender_id
              })
                .then(response => {
                  // 2s之后刷新
                  setTimeout(() => {
                    this.$message.success("进场成功！");
                    this.inloading = false;
                    // 刷新车辆列表
                    this.clickRefresh();
                  }, 2000);
                })
                .catch(error => {
                  this.$message.error("进场失败！");
                  this.inloading = false;
                });
            } else {
              const { weiList } = this.activeItem;
              let formForSend = { ...this.form };
              delete formForSend.weiList;
              let sendform = {
                loadometer: {
                  ...formForSend,
                  num: null,
                  tare_device_code: "",
                  load_weight: 0,
                  tare_device_num: "",
                  net_weight: 0,
                  gross_device_num: "",
                  leave_device_code: "",
                  gross_weight: 0,
                  gross_device_code: "",
                  creator: getUser(),
                  leave_time: "",
                  create_time: moment().format("YYYY-MM-DD HH:mm:ss"),
                  contract: "",
                  contract_id: "",
                  tare_time: "",
                  enter_device_code: "",
                  gross_time: "",
                  unit_price: 0,
                  enter_time: "",
                  tare_weight: 0,
                  leave_device_num: "",
                  enter_device_num: "",
                  order_id: "",
                  order_qty: 0,
                  status: "1",
                  vehicle_num: carNo,
                  initData: {},
                  sortObj: {}
                },
                formKey: "loadometer",
                templateKey: "loadometer"
              };

              if (
                weiList &&
                this.grossIndex == null &&
                this.tareIndex == null &&
                weiList.length > 1
              ) {
                this.$message.error("请选择对应的重车/空车重量！");
                return;
              }

              getList(
                sendform,
                `service/form/dataTemplate/v1/boSave/loadometer?delDraftId=`
              )
                .then(response => {
                  if (response.state) {
                    // let num = response.value.F_num;
                    let num = JSON.parse(response.value)[0].valuesMap.num;
                    // 异常车辆进场的话 需要刷新一下该车牌号在系统里的状态
                    if (this.activeType == "error") {
                      // 2、绑定新重量
                      if (this.grossIndex == null && this.tareIndex == null) {
                        // 2.1、重量都没选
                        // 2.1.1、只有一条重量记录
                        if (weiList.length == 1) {
                          getList(
                            {
                              location: "梨枫",
                              loadometer_num: num || 0,
                              vehicle_num: carNo,
                              device_code: weiList[0].device_code,
                              device_num: weiList[0].device_num,
                              type: ""
                            },
                            "/mine/loadometer/bind"
                          )
                            .then(response => {
                              if (response.state) {
                                // 绑定成功
                                this.$message.success(response.message);
                                // 抬杆
                                // getList(
                                //   { device_code: weiList[this.grossIndex].device_code },
                                //   "/mine/iot/lift"
                                // ).then(response => {});
                              } else {
                                this.$message.error(response.message);
                              }
                            })
                            .catch(error => {});
                        }
                      } else if (
                        this.grossIndex != null &&
                        this.tareIndex == null
                      ) {
                        // 2.2、只选择了毛重-重车的重量
                        getList(
                          {
                            location: "梨枫",
                            loadometer_num: num || 0,
                            vehicle_num: carNo,
                            device_code: weiList[this.grossIndex].device_code,
                            device_num: weiList[this.grossIndex].device_num,
                            type: "重车"
                          },
                          "/mine/loadometer/bind"
                        )
                          .then(response => {
                            if (response.state) {
                              // 绑定成功
                              this.$message.success(response.message);
                              // 抬杆
                              // getList(
                              //   { device_code: weiList[this.grossIndex].device_code },
                              //   "/mine/iot/lift"
                              // ).then(response => {});
                            } else {
                              this.$message.error(response.message);
                            }
                          })
                          .catch(error => {});
                      } else if (
                        this.tareIndex != null &&
                        this.grossIndex == null
                      ) {
                        // 2.3、只选择了皮重-空车的重量
                        getList(
                          {
                            location: "梨枫",
                            loadometer_num: num || 0,
                            vehicle_num: carNo,
                            device_code: weiList[this.tareIndex].device_code,
                            device_num: weiList[this.tareIndex].device_num,
                            type: "空车"
                          },
                          "/mine/loadometer/bind"
                        )
                          .then(response => {
                            if (response.state) {
                              // 绑定成功
                              this.$message.success(response.message);
                              // 抬杆
                              // getList(
                              //   { device_code: weiList[this.tareIndex].device_code },
                              //   "/mine/iot/lift"
                              // ).then(response => {});
                            } else {
                              this.$message.error(response.message);
                            }
                          })
                          .catch(error => {});
                      } else if (
                        this.grossIndex != null &&
                        this.tareIndex != null
                      ) {
                        // 2.4、毛重-重车 和 皮重-空车 都选择了
                        // 离现在时间久远的先绑定 再绑定离现在时间近的
                        let far = 0,
                          near = 0,
                          farType = "",
                          nearType = "";
                        if (this.tareIndex > this.grossIndex) {
                          far = this.tareIndex;
                          near = this.grossIndex;
                          farType = "空车";
                          nearType = "重车";
                        } else {
                          far = this.grossIndex;
                          near = this.tareIndex;
                          farType = "重车";
                          nearType = "空车";
                        }
                        getList(
                          {
                            location: "梨枫",
                            loadometer_num: num || 0,
                            vehicle_num: carNo,
                            device_code: weiList[far].device_code,
                            device_num: weiList[far].device_num,
                            type: farType
                          },
                          "/mine/loadometer/bind"
                        )
                          .then(response => {
                            if (response.state) {
                              // 绑定成功
                              this.$message.success(response.message);
                              // 抬杆
                              // getList(
                              //   { device_code: weiList[far].device_code },
                              //   "/mine/iot/lift"
                              // ).then(response => {});
                              getList(
                                {
                                  location: "梨枫",
                                  loadometer_num: num || 0,
                                  vehicle_num: carNo,
                                  device_code: weiList[near].device_code,
                                  device_num: weiList[near].device_num,
                                  type: nearType
                                },
                                "/mine/loadometer/bind"
                              )
                                .then(response => {
                                  if (response.state) {
                                    // 绑定成功
                                    this.$message.success(response.message);
                                    // 抬杆
                                    // getList(
                                    //   { device_code: weiList[near].device_code },
                                    //   "/mine/iot/lift"
                                    // ).then(response => {});
                                  } else {
                                    this.$message.error(response.message);
                                  }
                                })
                                .catch(error => {});
                            } else {
                              this.$message.error(response.message);
                            }
                          })
                          .catch(error => {});
                      }
                    }
                    // 2s之后刷新
                    setTimeout(() => {
                      this.$message.success("进场成功！");
                      this.inloading = false;
                      // 刷新车辆列表
                      this.clickRefresh();
                    }, 2000);
                  }
                })
                .catch(error => {
                  console.log(error);
                  this.$message.error("进场失败！");
                  this.inloading = false;
                });
            }
          }
        } else {
          this.$message.error("磅单内容有误，请修改！");
          return false;
        }
      });
    },
    //  确认出场，打印磅单
    async clickPrint() {
      if (this.activeType === "inner") {
        // 场内车辆不能打印
        this.$message.error("场内车辆不能打印磅单！");
        return;
      } else if (this.activeType == "recent") {
        this.$message.error("车辆已出场，不能打印磅单！");
        return;
      }
      // 请求接口获取打印数据
      if (!this.activeId) {
        this.$message.error("请点击选择一条准备出场的车牌号！");
        return;
      }
      await this.getInfo();
    },
    // 最近出场车辆-补打印
    async reClickPrint(item) {
      this.activeId = item.id_;
      this.activeItem = item;
      await this.getInfo();
      this.$refs.print.showModal(this.form);
    },
    // 最近出场车辆-点击表格内数据
    reviewCar(item) {
      this.clickPlateNo(item, "recent");
      this.closeListDrawer();
    },
    // 更新数据
    clickRefresh() {
      // 重新请求场内车辆，准备出场列表，
      // 清空表单数据
      this.loading = true;
      this.clickAdd();
      // 获取所有非异常车辆;
      this.getCarList();
      // 获取所有异常车辆
      this.getAbnormalList();
      // if (this.showTableDrawer) this.fetchPageData();

      this.isNew = false;
    },
    refreshActiveItem() {
      // 如果当前有选中的车辆 重新获取当前车辆的最新状态 以便按钮组进行变化
      if (this.activeId) {
        getRequest(
          `service/form/dataTemplate/v1/getForm/loadometer/loadometer?id=${
            this.activeId
          }&action=get&recordId=&dataKey=loadometer`
        ).then(response => {
          let info = response.data.loadometer || {};
          this.$set(this.activeItem, "status", info.status);
          this.$set(this.form, "status", info.status);
        });
      }
    },
    getCarList() {
      // 获取所有预约车辆
      this.getOrderList();
      // 获取所有场内车辆
      this.getInnerList();
      // 获取所有准备出场车辆
      this.getOutList();
      // 获取所有近期出场车辆
      this.getRecentList();
    },
    // refreshAbnormal(vehicle_num, device_code, device_num) {
    //   getList(
    //     { location: "梨枫", vehicle_num, device_code, device_num },
    //     "/mine/loadometer/bind"
    //   ).then(response => {
    //     console.log(response);
    //   });
    // },
    // 货物规格改变
    payloadChange(val) {
      let obj = this.goodSpec.find(item => item.payload_spec == val);
      if (obj) {
        this.$set(this.form, "payload_id", obj.id_);
      } else {
        this.$set(this.form, "payload_id", "");
      }
    },
    // 车牌数字、backspace、字母下拉选择内容
    queryLicenseSearch(queryString, cb) {
      var results = [];
      // 先数字
      this.numList.map(e => {
        results.push({
          value: e
        });
      });
      // 再backspace
      results.push({
        value: "← 删除"
      });
      // 再字母
      this.letterList.map(e => {
        results.push({
          value: e
        });
      });
      cb(results);
    },
    // 选择身份判断是否要自动打开车牌字母数字的下拉
    handleProSelect() {
      if (this.lastLicenseInput.length < 6) {
        this.$nextTick(() => {
          this.$refs.license.focus();
        });
      }
      this.checkLicenseSelect();
    },
    // 选择车牌字母数字部分
    handleLicenseSelect(item) {
      if (item.value && item.value.indexOf("删除") > -1) {
        // 选择backspace 删除最后一位
        if (this.lastLicenseInput.length > 0) {
          this.form.licensePlate = this.lastLicenseInput.slice(
            0,
            this.lastLicenseInput.length - 1
          );
        } else {
          this.form.licensePlate = this.lastLicenseInput;
        }
      } else {
        // 选择数字、字母 在最后添加
        this.form.licensePlate = this.lastLicenseInput + "" + item.value;
      }
      if (
        this.form.licensePlate != undefined &&
        this.form.licensePlate.length < 6
      ) {
        this.$nextTick(() => {
          this.$refs.license.focus();
        });
      }
      this.checkLicenseSelect();
    },
    checkLicenseSelect() {
      const { province, licensePlate } = this.form;
      let { time, weight, device_num, weiList } = this.activeItem;
      let carNo = province + licensePlate;

      // 先判断 预约车辆+场内车辆+未打印的准备出场 中是否已含有
      let carList = [
        ...this.orderList.map(e => e.vehicle_num),
        ...this.innerList.map(e => e.vehicle_num),
        ...this.outList.filter(e => e.status == 8).map(e => e.vehicle_num)
      ];
      if (carList.includes(carNo)) {
        // 若有 询问是否代入已有值 货物名称、销售采购、发货单位、收货单位、车牌号
        this.$confirm(
          "该车辆已预约/进场，是否将货物、单位、称重信息填入",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            // 获取场内已有相同的车牌号的信息
            let info = [
              ...this.orderList,
              ...this.innerList,
              ...this.outList.filter(e => e.status == 8)
            ].filter(e => e.vehicle_num == carNo);
            if (!this.activeId) {
              // 点击【新建磅单】-选择车牌号
              if (info[0].status == 0) {
                this.clickPlateNo(info[0], "order");
              } else if (info[0].status == 8) {
                this.clickPlateNo(info[0], "outer");
              } else {
                this.clickPlateNo(info[0], "inner");
              }
              // } else {
              //   // 预约的可以进场
              //   this.form = {
              //     ...info[0],
              //     province,
              //     licensePlate
              //   };
              //   this.$refs.form.resetFields();
              // }
            } else if (this.activeType == "error") {
              // 点击【异常车辆】-选择车牌号
              this.grossIndex = null;
              this.tareIndex = null;

              // 同车牌号的称重数据插入list
              if (info[0].gross_device_num && info[0].gross_device_code) {
                if (
                  weiList.find(e => e.device_num == info[0].gross_device_num)
                ) {
                  //
                } else {
                  weiList.push({
                    time: info[0].gross_time,
                    weight: info[0].gross_weight,
                    device_num: info[0].gross_device_num,
                    device_code: info[0].gross_device_code,
                    type: "gross"
                  });
                }
              }
              if (info[0].tare_device_num && info[0].tare_device_code) {
                if (
                  weiList.find(e => e.device_num == info[0].tare_device_num)
                ) {
                  //
                } else {
                  weiList.push({
                    time: info[0].tare_time,
                    weight: info[0].tare_weight,
                    device_num: info[0].tare_device_num,
                    device_code: info[0].tare_device_code,
                    type: "tare"
                  });
                }
              }
              // 按照时间排序
              weiList.sort(this.dateData("time", false));
              // 获取重车和空车的下标默认值
              if (info[0].gross_device_num && info[0].gross_device_code) {
                weiList.map((e, i) => {
                  if (e.device_num == info[0].gross_device_num) {
                    this.grossIndex = i;
                  }
                });
              }
              if (info[0].tare_device_num && info[0].tare_device_code) {
                weiList.map((e, i) => {
                  if (e.device_num == info[0].tare_device_num) {
                    this.tareIndex = i;
                  }
                });
              }
              // 设置空车和重车的下标默认值
              if (this.grossIndex != null && this.tareIndex != null) {
                //
              } else {
                this.intTypeIndex(info[0].order_type, weiList);
              }

              // 预约磅单没有customer_vender_id
              if (!info[0].customer_vender_id) {
                let list =
                  info[0].order_type == 1 ? this.customers : this.suppliers;
                let name =
                  info[0].order_type == 1 ? info[0].customer : info[0].vender;
                info[0].customer_vender_id = list.find(e => e.name == name).id_;
              }

              this.form = {
                time,
                weight,
                weiList,
                gross_device_num: device_num,
                payload: info[0].payload || "",
                payload_id: info[0].payload_id || "",
                order_type: info[0].order_type,
                vender: info[0].vender || "",
                customer: info[0].customer || "",
                customer_vender_id: info[0].customer_vender_id || "",
                province,
                licensePlate
              };
              this.$refs.form.resetFields();
            }
          })
          .catch(() => {});
      }
    },

    // property是你需要排序传入的key,bol为true时是升序，false为降序
    dateData(property, bol) {
      return function(a, b) {
        var value1 = a[property];
        var value2 = b[property];
        if (bol) {
          // 升序
          return Date.parse(value1) - Date.parse(value2);
        } else {
          // 降序
          return Date.parse(value2) - Date.parse(value1);
        }
      };
    },

    // 默认空车、重车
    intTypeIndex(order, list) {
      if (this.grossIndex == null) {
        // 找毛重-重车的下标
        if (order == 1) {
          // 销售
          // 空车进 重车出
          for (var i = 0; i < list.length; i++) {
            if (this.tareIndex != i) {
              if (list[i].origin == "fullLocation") {
                this.grossIndex = i;
                break;
              }
            }
          }
        } else {
          // 采购
          // 重车进 空车出
          for (var i = 0; i < list.length; i++) {
            if (this.tareIndex != i) {
              if (list[i].origin == "emptyLocation") {
                this.grossIndex = i;
                break;
              }
            }
          }
        }
      }
      if (this.tareIndex == null) {
        // 找皮重-空车的下标
        if (order == 1) {
          // 销售
          // 空车进 重车出
          for (var i = 0; i < list.length; i++) {
            if (this.grossIndex != i) {
              if (list[i].origin == "emptyLocation") {
                this.tareIndex = i;
                break;
              }
            }
          }
        } else {
          // 采购
          // 重车进 空车出
          for (var i = 0; i < list.length; i++) {
            if (this.grossIndex != i) {
              if (list[i].origin == "fullLocation") {
                this.tareIndex = i;
                break;
              }
            }
          }
        }
      }
    },

    //生成随机数
    randomString(e) {
      e = e || 6;
      var t = "ABCDEF",
        a = t.length,
        n = "";
      for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
      return n;
    },
    checkAcquirepicture(type, device_code, device_num) {
      this.weightPicType = type;
      if (!this.activeId || this.activeId != "") {
        let code = "",
          num = "";

        if (type == "error" && device_code && device_num) {
          const { time, vehicle_num, weight, origin } = this.activeItem;
          this.picForm.vehicle_num = vehicle_num;
          this.picForm.time = time;
          this.picForm.weight = weight;
          this.picForm.origin = origin;
          this.picForm.device_code = device_code;
          this.picForm.device_num = device_num;
          code = device_code;
          num = device_num;
        } else if (type == "gross") {
          const {
            vehicle_num,
            gross_time,
            gross_weight,
            gross_device_code,
            gross_device_num
          } = this.activeItem;

          this.picForm.vehicle_num = vehicle_num;
          this.picForm.time = gross_time;
          this.picForm.weight = gross_weight;
          this.picForm.device_code = gross_device_code;
          this.picForm.device_num = gross_device_num;
          code = gross_device_code;
          num = gross_device_num;
        } else if (type == "tare") {
          const {
            vehicle_num,
            tare_time,
            tare_weight,
            tare_device_code,
            tare_device_num
          } = this.activeItem;

          this.picForm.vehicle_num = vehicle_num;
          this.picForm.time = tare_time;
          this.picForm.weight = tare_weight;
          this.picForm.device_code = tare_device_code;
          this.picForm.device_num = tare_device_num;
          code = tare_device_code;
          num = tare_device_num;
        }

        if (this.picForm.weightPicMin.length > 0) {
          this.picForm.weightPicMin = [""];
        } else {
          this.picForm.weightPicMin = [];
        }
        this.picloading = true;
        getList(
          {
            device_code: code,
            device_num: num
          },
          "/mine/iot/capture"
        )
          .then(response => {
            if (response.state) {
              this.picForm.weightPicMin =
                response.value.images.map(e => {
                  if (e && e.indexOf("?") > -1) {
                    return e + "&height=170";
                  } else {
                    return e + "?height=170";
                  }
                }) || [];
              this.picForm.weightPic = response.value.images || [];
              this.picShow = true;
            } else {
              this.picForm.weightPicMin = [];
              this.picForm.weightPic = [];
              this.$message.error(response.message);
            }
          })
          .catch(error => {
            this.picForm.weightPicMin = [];
            this.picForm.weightPic = [];
            this.$message.error(response.message);
          })
          .finally(() => {
            setTimeout(() => {
              this.picloading = false;
            }, 1000);
          });
      } else {
        this.$message.error("请选择具体车辆");
      }
    },

    checkEntrancePic(enter_num, enter_code, leave_num, leave_code) {
      if (Boolean(this.activeId)) {
        const { vehicle_num, create_time, leave_time } = this.activeItem;
        this.entrancePicForm.vehicle_num = vehicle_num;
        this.entrancePicForm.enter_time = create_time;
        this.entrancePicForm.leave_time = leave_time;

        if (Boolean(enter_num) && Boolean(enter_code)) {
          this.entrancePicForm.enter.device_code = enter_code;
          this.entrancePicForm.enter.device_num = enter_num;
        }
        if (Boolean(leave_num) && Boolean(leave_code)) {
          this.entrancePicForm.leave.device_code = leave_code;
          this.entrancePicForm.leave.device_num = leave_num;
        }
        this.entrancePicShow = true;
      } else {
        this.$message.error("请选择具体车辆");
      }
    },
    entrancePicClose() {
      this.entrancePicShow = false;
      this.entrancePicForm = {
        vehicle_num: 0,
        enter_time: 0,
        leave_time: 0,
        enter: {
          device_code: "",
          device_num: "",
          weightPicMin: [],
          weightPic: []
        },
        leave: {
          device_code: "",
          device_num: "",
          weightPicMin: [],
          weightPic: []
        }
      };
    },
    picClose() {
      this.picShow = false;
      this.picForm = {
        vehicle_num: 0,
        time: 0,
        weight: 0,
        origin: "",
        weightPicMin: [],
        weightPic: []
      };
    }
  }
};
</script>
<style lang="scss" scoped>
.space_between {
  justify-content: space-between;
}
.space_around {
  justify-content: space-around;
}

.flex {
  display: flex;
  align-items: flex-start;
}

.w_full {
  width: 100%;
}

.container {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  position: relative;
  z-index: 2;
}
.carimg {
  width: 300px;
  padding-left: 20px;
  flex-shrink: 0;
  color: #3344dd;
}
.weighting_room {
  background: #f7f7f7;
  border: 1px solid #6b88bf;
  font-size: 32px;
  color: #333;
  height: calc(100vh - 128px);
  overflow: auto;

  .box {
    padding: 10px 20px;
    background: #fff;
    margin-top: 10px;
    align-items: center;

    &:first-of-type {
      margin-top: 0;
    }

    .box_label {
      position: relative;
      color: #3344dd;
      // width: 180px;
      margin: 0 20px 0 30px;
      flex-shrink: 0;
      font-size: 32px;
      font-weight: bold;

      &::before {
        content: "";
        position: absolute;
        left: -28px;
        top: 10px;
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: #3344dd;
      }
    }

    .invehicle_num_box {
      max-height: 120px;
      overflow-y: auto;
      overflow-x: hidden;

      button {
        font-weight: bold;
      }
    }

    .vehicle_num_box {
      max-height: 204px;
      overflow-y: auto;
      overflow-x: hidden;

      button {
        font-weight: bold;
      }
    }

    .tool_btn {
      position: fixed;
      width: 100%;
      bottom: 0;
      left: 0;
      right: 0;
      display: flex;
      justify-content: space-between;
      background-color: #fff;
      padding: 20px;

      border-top: #6b88bf 1px solid;

      button {
        font-size: 20px;
        font-weight: bold;
      }
    }

    .platenum {
      min-width: 180px;
      margin: 5px;
      text-align: left;
    }

    .lightblue_btn {
      border-color: #d9e8fe;
      background: #d9e8fe;
      color: #3344dd;
    }

    .blue_btn {
      border-color: #3344dd;
      background: #3344dd;
      color: #fff;
    }

    .lightgray_btn {
      border-color: #d9d9d9;
      background: #d9d9d9;
      color: #a4a4a4;
    }

    .gray_btn {
      border-color: #999999;
      background: #999999;
      color: #f3f3f3;
    }

    .lightred_btn {
      border-color: #fedbd9;
      background: #fedbd9;
      color: #fa5858;
    }

    .lightgreen_btn {
      border-color: #cfedc1;
      background: #cfedc1;
      color: #159f3b;
    }

    .green_btn {
      border-color: #159f3b;
      background: #159f3b;
      color: #fff;
    }

    .errorbtn {
      position: relative;

      &::after {
        content: "!";
        position: absolute;
        right: -1px;
        top: -1px;
        display: inline-block;
        width: 24px;
        height: 24px;
        border-radius: 50%;
        text-align: center;
        line-height: 20px;
        font-weight: bold;
        background: #fa5858;
        color: #fff;
        border: 2px solid #fff;
        box-shadow: 0px 0px 3px 0px #000000;
      }
    }

    .more_btn {
      color: #333;
      background: #ededed;
      border-color: transparent;
      &:hover {
        color: #4273fe;
        background: #333;
      }
    }

    .error_weight_box {
      position: relative;
      font-size: 24px;
      padding: 20px;
      width: 100%;
      .error_mask {
        position: absolute;
        background: rgba(217, 217, 217, 0.9);
        height: calc(100% - 20px);
        width: calc(100% - 20px);
        text-align: center;
      }
      .result_text {
        color: #999999;
        line-height: 30px;
        margin-bottom: 10px;
      }
      .error_weight_content {
        height: calc(100% - 40px);
        width: 100%;
        max-height: 300px;
        overflow-y: auto;
        .weight_item {
          background: #fff;
          border-radius: 8px;
          padding: 10px 15px;
          width: 100%;
          height: 120px;
          margin-bottom: 10px;
          .weight_info_box {
            width: calc(100% - 140px);
            padding-right: 15px;
            .num {
              color: #ff5151;
              font-size: 64px;
              font-weight: bold;
            }
            .time {
              color: #999;
              font-size: 20px;
            }
            button {
              font-size: 20px;
              line-height: 1.3em;
              text-align: center;
              padding: 6px 24px;
              border-radius: 8px;
              border-color: #3344dd;
              color: #3344dd;
              font-weight: bold;
              background: #fff;
            }
          }
          .weight_btn_box {
            width: 140px;
            height: 100%;
            padding-left: 15px;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            border-left: 1px dashed #999;

            button {
              border: 1px solid #d9d9d9;
              font-size: 18px;
              text-indent: 1em;
              letter-spacing: 1em;
              line-height: 1.3em;
              text-align: center;
              background: #fff;
              margin: 0;
              padding: 6px 15px;
              border-radius: 4px;

              &.weight_yellow {
                color: #ff9900;
                &.active {
                  color: #fff;
                  background: #ff9900;
                }
              }

              &.weight_blue {
                color: #3b90df;
                &.active {
                  color: #fff;
                  background: #3b90df;
                }
              }

              &.disable {
                color: #666666;
                background: #d9d9d9;
              }

              &.notallowed {
                color: #666666;
                background: #d9d9d9;
                cursor: not-allowed;
              }
            }
          }
        }
      }
    }
  }

  .enterDialog,
  .leaveDialog {
    .pic_text {
      color: #fa5858;
      font-size: 28px;
      line-height: 40px;
      margin-bottom: 10px;
      margin-top: -10px;
    }
    .pic_info_box {
      margin-bottom: 20px;
      .info_item {
        line-height: 35px;
        color: #333;
        font-size: 28px;
        margin-bottom: 20px;
        .key {
          display: inline-flex;
          justify-content: space-between;
          width: 100px;
          color: #999999;
          margin-right: 20px;
          text-align: center;
        }
      }
    }

    .pic_viewer {
      max-height: 400px;
      overflow: auto;
      margin: 0 -5px;
      position: relative;
      .refresh_btn {
        position: absolute;
        right: 0px;
        top: 0px;
        border-color: #fff;
        background: #fff;
        color: #666;
      }
      .title {
        color: #3344dd;
        font-size: 24px;
        line-height: 30px;
        margin-left: 30px;
        font-weight: bold;
        position: relative;
        &::before {
          content: "";
          position: absolute;
          left: -20px;
          top: 10px;
          display: inline-block;
          width: 10px;
          height: 10px;
          border-radius: 50%;
          background: #3344dd;
        }
      }
      .img {
        width: calc(50% - 10px);
        height: 170px;
        margin: 5px;
        cursor: pointer;
      }
    }

    .pic_toolbar {
      border-top: 1px solid #eee;
      padding-top: 20px;
      margin-top: 20px;
      display: inline-flex;
      justify-content: end;
      width: 100%;
      button {
        font-size: 24px;
        text-indent: 0.2em;
        letter-spacing: 0.2em;
        text-align: center;
        margin-left: 20px;
      }
    }
  }
}

::v-deep {
  .el-input-group__prepend {
    border: 1px solid #bfbfbf;
    border-right: 0;
  }

  .el-input.is-disabled .el-input__inner {
    background-color: #f7f7f7;
    color: #333333;
    border-color: #bfbfbf;
  }

  .el-input-group__prepend div.el-select .el-input__inner {
    border: 1px;
    height: 30px;
    color: #333333;
    background-color: #fff;
  }

  .el-input-group__prepend div.el-select .is-disabled .el-input__inner {
    background-color: #f7f7f7;
  }

  .el-input-group__append {
    color: #333333;
  }

  .el-input--medium .el-input__inner {
    background: #fff;
  }
  .time_row {
    width: 60%;
    margin-right: 20px;
    min-width: 11em;
    display: inline-block;
  }
  .import_num {
    width: 60%;
    margin-right: 20px;
    min-width: 11em;
    .el-input__inner {
      color: #ff5151 !important;
      font-size: 48px;
      font-weight: bold;
    }
  }

  .el-form {
    background: #f7f7f7;
    padding: 10px 20px;
  }
  .el-select-dropdown {
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.5);
  }
  .el-form-item__label,
  .el-select-dropdown__item,
  .el-form-item__content {
    color: #333;
    font-size: 32px;
  }

  .el-button--medium,
  .el-input--medium,
  .el-select .el-input .el-select__caret {
    font-size: 28px;
  }

  .el-form-item--medium .el-form-item__content,
  .el-form-item--medium .el-form-item__label,
  .el-input--medium .el-input__inner,
  .el-input--medium .el-input__icon,
  .el-select-dropdown__item {
    line-height: 49px;
  }

  .el-input--medium .el-input__inner,
  .el-select-dropdown__item {
    height: 50px;
    text-indent: 0.3em;
  }

  .el-dialog {
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.8);
    .el-dialog__headerbtn {
      font-size: 22px;
      background: rgba(0, 0, 0, 0.3);
      // background: #fedbd9;
      width: 26px;
      height: 26px;
      .el-dialog__close {
        color: #000;
        font-weight: bold;
        // color: #fa5858;
      }
    }
  }

  .orderRadio {
    .el-radio {
      margin: 0;
      height: 50px;
      padding: 0px 10px;
      line-height: 50px;
      .el-radio__label {
        font-size: 26px;
        color: #4273fe;
      }
      .el-radio__input {
        line-height: 50px;
        &.is-checked {
          .el-radio__inner {
            border-color: #4273fe;
            background: #4273fe;
          }
        }

        .el-radio__inner {
          height: 20px;
          width: 20px;
          &::after {
            height: 10px;
            width: 10px;
          }
        }
      }

      &.is-bordered.is-checked {
        border-color: #4273fe;
      }
    }
  }

  .province_box {
    min-width: 300px;
  }

  .license_box {
    min-width: 150px;
  }

  .picDialog {
    .el-dialog__title {
      line-height: 39px;
    }
    .el-dialog__body {
      padding: 0;
    }
  }

  .enterDialog {
    margin-left: 10px;
    width: calc(50% - 20px);
  }
  .leaveDialog {
    margin-right: 10px;
    width: calc(50% - 20px);
  }

  .weight_item {
    .el-form-item__label,
    .el-form-item__content,
    .el-input--medium,
    .el-input--medium .el-input__inner {
      font-weight: normal;
      font-size: 24px;
      line-height: 34px;
    }
    .el-input--medium .el-input__inner {
      height: 36px;
    }
    .el-form-item {
      margin-bottom: 10px;
      button {
        font-size: 18px;
        padding: 8px 15px;
        text-indent: 1em;
        letter-spacing: 1em;
      }
    }
  }
}
</style>
<style lang="scss">
// 表单下拉元素均在body下 用全局样式覆盖
.customer_select_style,
.payload_select_style,
.province_select_style {
  li {
    font-size: 32px;
    height: 50px;
    line-height: 50px;
  }
}

.province_select_style {
  width: 850px !important;

  .el-select-dropdown__list {
    display: flex;
    flex-wrap: wrap;
  }
}

.license_select_style {
  width: 810px !important;

  .el-autocomplete-suggestion__list {
    display: flex;
    flex-wrap: wrap;

    li {
      font-size: 32px;
      min-width: 66px;
      line-height: 50px;
      text-align: center;
    }
  }
}
</style>
